package allod.camera

import swing.Button
import javax.swing.{Icon, ImageIcon}
import java.util.ResourceBundle
import collection.mutable

/**
 * Created by IntelliJ IDEA.
 * User: allod
 * Date: 5 квіт 2010
 * Time: 19:50:27
 */

sealed abstract class CameraStatus {
  def prepareButton(button: Button): Unit

  def getNextStatus: CameraStatus
}
object CameraStatus {
  private val loader = getClass().getClassLoader()
  private val resources: mutable.Map[String, Icon] = mutable.Map.empty[String, Icon]
  private val iconsRes = ResourceBundle.getBundle("icons")

  def isActive(status: CameraStatus): Boolean = {
    return status == BLOCKED || status == PLAYING
  }

  def loadIcon(iconName: String): Icon = {
    if (resources.contains(iconName)) {
      return resources(iconName)
    } else {
      val icon = new ImageIcon(loader.getResource(iconsRes.getString(iconName)))
      resources += iconName -> icon
      return icon
    }
  }

  case object INIT extends CameraStatus {
    def prepareButton(button: Button): Unit = {
      button.enabled = false
      button.icon = loadIcon("camera")
    }

    def getNextStatus: CameraStatus = {
      throw new AssertionError("Should never rich here")
    }
  }
  case object BLOCKED extends CameraStatus {
    def prepareButton(button: Button): Unit = {
      button.enabled = false
      button.icon = loadIcon("blocked")
    }

    def getNextStatus: CameraStatus = {
      return INIT
    }
  }
  case object READY_TO_RECORD extends CameraStatus {
    def prepareButton(button: Button): Unit = {
      button.enabled = true
      button.icon = loadIcon("record")
    }

    def getNextStatus: CameraStatus = {
      return BLOCKED
    }
  }
  case object PLAYING extends CameraStatus {
    def prepareButton(button: Button): Unit = {
      button.enabled = true
      button.icon = loadIcon("pause")
    }

    def getNextStatus: CameraStatus = {
      return PAUSED
    }
  }
  case object PAUSED extends CameraStatus {
    def prepareButton(button: Button): Unit = {
      button.enabled = true
      button.icon = loadIcon("play")
    }

    def getNextStatus: CameraStatus = {
      return PLAYING
    }
  }
}